package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.train.bean.TrainCourseDetailBean;

/**
 * Created by dllo on 16/1/25.
 */
public class TrainCourseDetailWorkoutsAdapter extends BaseAdapter {
    private Context context;
    private TrainCourseDetailBean data;

    public TrainCourseDetailWorkoutsAdapter(Context context, TrainCourseDetailBean data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.getData().getResults().getWorkouts() == null ? 0 : data.getData().getResults().getWorkouts().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_fragment_train_workouts,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        myViewHolder.tvTitle.setText(data.getData().getResults().getWorkouts().get(position).getName());
        myViewHolder.tvDifficulty.setText("K" + data.getData().getResults().getWorkouts().get(position).getDifficulty());
        switch (data.getData().getResults().getWorkouts().get(position).getDifficulty()) {
            case 1:
                myViewHolder.tvFoundations.setText("零基础");
                break;
            case 2:
                myViewHolder.tvFoundations.setText("初学");
                break;
            case 3:
                myViewHolder.tvFoundations.setText("进阶");
                break;
            case 4:
                myViewHolder.tvFoundations.setText("强化");
                break;
            case 5:
                myViewHolder.tvFoundations.setText("挑战");
                break;
        }
        myViewHolder.tvTime.setText(data.getData().getResults().getWorkouts().get(position).getDuration() + "分钟");
        String content = "";
        for (int i = 0; i < data.getData().getResults().getWorkouts().get(position).getTrainingpoints().size(); i++) {
            content = data.getData().getResults().getWorkouts().get(position).getTrainingpoints().get(i).getName() + " ";
        }
        myViewHolder.tvBody.setText(content);
        myViewHolder.tvJoin.setText(data.getData().getResults().getWorkouts().get(position).getPioneer() + "人已参加");
        myViewHolder.ratingBar.setRating((float) data.getData().getResults().getWorkouts().get(position).getDifficulty());
        myViewHolder.tvEquipments.setText(data.getData().getResults().getWorkouts().get(position).getEquipments().get(0).getName());

        return convertView;
    }

    class MyViewHolder {
        private TextView tvTitle,tvDifficulty,tvFoundations,tvEquipments,tvTime,tvBody,tvJoin;
        private RatingBar ratingBar;
        public MyViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_item_train_workouts_title);
            tvDifficulty = (TextView) view.findViewById(R.id.tv_item_train_workouts_difficulty);
            tvFoundations = (TextView) view.findViewById(R.id.tv_item_train_workouts_foundations);
            tvEquipments = (TextView) view.findViewById(R.id.tv_item_train_workouts_equipments);
            tvTime = (TextView) view.findViewById(R.id.tv_item_train_workouts_time);
            tvBody = (TextView) view.findViewById(R.id.tv_item_train_workouts_whole_body);
            tvJoin = (TextView) view.findViewById(R.id.tv_item_train_workouts_join_person_number);
            ratingBar = (RatingBar) view.findViewById(R.id.rating_item_train_workouts_star);
        }
    }
}
